package wili.b1.transform;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import wili.attrMapping.file.JsonFile;
import wili.c.Ca;
import wili.json.JsonNode;
import wili.json.JsonObj;

class FindGuid9 {

	public static void main(String[] args) {
		//找到 grp父的guid, 根据guid找到nodes, 操作node的edit属性值att。 set by(grp)
		String[] tt = { //"GroupAlwaysEditable", "#GROUP_ALWAYS_EDITABLE#",
				"deliveryandARInvoiceDisable", "#HAS_JE_DOC_DISABLE#" };
		JsonFile guidFrom = new JsonFile(new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\f\\SalesDoc.detailView_acc.json"));
		JsonFile byGuid = new JsonFile(new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\accountingTab_AR.fragment.json"));
		for (int i = 0; i < tt.length; i += 2) {
			final String toValue = tt[i + 1];
			List<JsonObj> oldGrpDads = guidFrom.findObjs_byAtt("formulaGroup", tt[i]);
			Set<String> guidSet = oldGrpDads.stream().map(ele -> ele.dad.att("guid").stringValue)
					.collect(Collectors.toSet());

			byGuid.root.traverse(new Predicate<JsonNode>() {
				@Override
				public boolean test(JsonNode jsonNode) {
					String EDITABLE = "editable";
					if (jsonNode instanceof JsonObj) {
						JsonObj obj = (JsonObj) jsonNode;
						String st = obj.valueOfSon("guid", "");
						if (guidSet.contains(st)) {
							JsonNode editNode = obj.att(EDITABLE);
							if (editNode == null)
								return false;
							if (!editNode.stringValue.contains(toValue)) {
								st = editNode.stringValue;
								if (st.startsWith("{=")) {
									String st2 = st.replaceAll("#\\w+#\\s*&& ", "");
									if (st2.equals(st)) {
										st = st.replaceAll("#\\w+#", "");
									} else {
										st = st2;
									}
									st = "{= " + toValue + " && " + st.substring(2);
									st = st.replaceFirst("#\\s+&&\\s+\\}", "#}");
									editNode.setStringValue(st);
								} else {
									Ca.asert(false, editNode.toString());
								}
							}
						}
					}
					return false;
				}
			});

		}
		String st = byGuid.refreshJsonSt();
		Ca.log("\n\n\n==================================\n" + st);

	}
}
